/*#include <msp430g2231.h>

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;         // Stop watchdog timer
  P1DIR=0x41;
 while(1)
 {
  P1OUT ^= 0x01; 		  // green LED on
  _delay_cycles(50000);                
  P1OUT ^= 0x40; 	         // red LED on   	  
  _delay_cycles(50000);
 }
}
  */
//  T32SPI22.c  SPI  Configuration and Send out 1 byte

#include <msp430g2231.h>
#define RED    (1<<0)             // Red LED is at bit 0 of Port 1
#define GREEN  (1<<6)             // Green LED is at bit 6 of Port 1
#define BUTTON (1<<3)             // Push Button is at bit 3 of Port 1
int ms, SEC, M ;
void SystemInit(void);

void main(){
       int i,j,k ;
             
       SystemInit();

       while( (P1IN&BUTTON)!=0 ); // Wait here if button not pressed
       while( (P1IN&BUTTON)==0 ); // Wait here till button is released

       ms = 0 ;                          // TimerA  1 ms interrupts
       TACTL = TASSEL1 + MC0 + TAIE  ;   // SMCLK, UP, Interrupt enabled 
       // Hardware Interrupt at P1.3  Already configured as push button
       P1IES &= ~BUTTON ;                // Rising Edge
       M = 5 ;                                  // Blink 5 times, if button not pressed
       P1IFG &= ~BUTTON ;                // Clear P1.3 Flag
       P1IE |= BUTTON ;                  // P1.3 interrupt enabled

                                  // BCSCTL1 as before (default) Low frequency  /1
       BCSCTL3 |= XCAP_3 ;        // 32768-Hz, 12.5 pF
       WDTCTL = WDT_ADLY_1000;    // WDT 1000ms interval timer
       SEC = 0 ;    
       IE1 |= WDTIE;                     // Enable WDT interrupt

       _enable_interrupts();             // or use _BIS_SR(GIE);  see SLAU132G
      
       while(1){                  // Run this if red LED not used
              for(k=1;k<=M;k++){
                     P1OUT |=  RED ;           
                     for(i=0;i<=100;i++){for(j=0;j<=1000;j++);}
                     P1OUT &= ~RED ;     
                     for(i=0;i<=200;i++){for(j=0;j<=1000;j++);}
              }
              for(i=0;i<=1000;i++){for(j=0;j<=1000;j++);}
       }            
      
}


#pragma vector=WDT_VECTOR
__interrupt void WDTISR(void){
                     SEC++;
}

#pragma vector=PORT1_VECTOR              // at 0xFFE4
__interrupt void HWIP13ISR(void){
       if((P1IFG&BUTTON)==BUTTON){       // Button causing interrupt
              P1IFG &= ~BUTTON ;                // Clear Flag
              M++;
              if(M>10) M = 3 ;
       }
}

#pragma vector=TIMERA1_VECTOR            // at 0xFFF0
__interrupt void TimerAISR(void){
       if(TAIV==TAIV_TAIFG){                    // Using TAIFG (Timer overflow)
              ms++ ;
       }     
}

void SystemInit(void){
       // Stop watchdog timer
       WDTCTL = WDTPW + WDTHOLD; 
       // GPIO Configuration for bit 0 Red LED
       P1SEL &= ~RED ;            // 0 for I/O function
       P1DIR |=  RED ;            // 1 for output
       P1OUT &= ~RED ;            // Clear the bit
       // GPIO Configuration for bit 6 Green LED
       P1SEL &= ~GREEN ;    // 0 for I/O function
       P1DIR |=  GREEN ;    // 1 for output
       P1OUT &= ~GREEN ;    // Clear the bit
       // GPIO Configuration for bit 3 Push Button
       P1SEL &= ~BUTTON ;   // 0 for I/O function
       P1DIR &= ~BUTTON ;   // 0 for input
       P1REN |=  BUTTON ;   // 1 to enable PUPD
       P1OUT |=  BUTTON ;   // 1 to Pull Up
      
       // SMCLK & Timer A Configuration
                                  // DCOCTL at default value 01100000  DCOx = 3
       BCSCTL1 &= ~(0x0F) ;        // BCSCTL1=xxxx0000 Mask off RSELx bits
       BCSCTL1 |= 13 ;             // BCSCTL1=xxxx1101 or RSELx = 13 for 8-MHz
//     DCOCTL        |= 16 ;                     // MODx = 16  (/32)  For calibration
       TACCR0 = 7999 ;                                 // 8000/8000000 = 1 ms

       // USI configured as SPI
       USICTL0  = USIPE6+USIPE5+USIMST+USIOE+USISWRST; // SDI not used
       USICTL1 = USICKPH;                // 10000001  Phase = 1
       USICKCTL = USISSEL_2;             // 00001000  /1, SMCLK, (Clk=0)
       USICTL0 &= ~USISWRST;                    // Release USI
       while((USICTL1&USIIFG)==0);       // Wait till USICNTx==0
       USISRL = 0x4F ;                   // Just show something '3'
       USICNT = 8 ;                      // Send 8 bits      
}